/** 
* Descripttion:  Copyright (c) 2020 Sprocomm. All rights reserved.
* version:  V1.0 
* Author:  Sprocomm
* Date: 2020-10-26 09:47:14
* LastEditors:  Sprocomm
* LastEditTime: 2020-10-26 09:50:25
**/
#include <memory.h>
#include "imgtool.h"

void PicRotary2(TPicRegion* Dst,TPicRegion* Src,int Angle,double ZoomX,double ZoomY,double move_x,double move_y)
{
	double RotaryAngle=Angle*PI;
	RotaryAngle=RotaryAngle/180.0;
    double Dstrx=Dst->width*0.5;  
    double Dstry=Dst->height*0.5; 
    double Srcrx=Src->width*0.5;  
    double Srcry=Src->height*0.5; 
		double sinA=sin(RotaryAngle);
	double cosA=cos(RotaryAngle);

    for (long y=0;y<Dst->height;++y)
    {
        for (long x=0;x<Dst->width;++x)
        {
            long srcx=(long)((x-Dstrx-move_x)/ZoomX*cosA - (y-Dstry-move_y)/ZoomY*sinA + Srcrx) ;
            long srcy=(long)((x-Dstrx-move_x)/ZoomX*sinA + (y-Dstry-move_y)/ZoomY*cosA + Srcry) ;
			if ((srcx>=0)&&(srcx<Src->width) && (srcy>=0)&&(srcy<Src->height))
			{
				memcpy(Dst->pdata+Dst->byte_width*y+3*x,Src->pdata+Src->byte_width*srcy+3*srcx,3);
			}
			else 
			{	
					Dst->pdata[Dst->byte_width*y+3*x]=0x00;
					Dst->pdata[Dst->byte_width*y+3*x+1]=0x00;
					Dst->pdata[Dst->byte_width*y+3*x+2]=0x00;
			}
        }
    }

}
void PicCrop(TPicRegion* Dst,TPicRegion* Src,int *CropCoorL,int *CropCoorR)
{
	int i=0;
	int j=0;
	int temp=CropCoorL[1];
	CropCoorL[1]=Src->height-CropCoorR[1];
	CropCoorR[1]=Src->height-temp;
	for (long y=CropCoorL[1];y<=CropCoorR[1];++y)
    {
        i=0;
		for (long x=CropCoorL[0];x<=CropCoorR[0];++x)
        {
			memcpy(Dst->pdata+Dst->byte_width*j+3*i,Src->pdata+Src->byte_width*y+3*x,3);	
			i++;
		}
		j++;
	}
}
void usage(void)
{
	printf("/nUsage: imgtool /n");
	printf("              [-o ExitImageName]            Open the exiting image/n");
	printf("              [-r angle | -c x1,y1 x2,y2]   Rotate some angles or Crop from top left corner to right down corner/n");
	printf("              [-s NewImageName]             Save the result by a new image name/n");
	printf("              [-z ZoomX,ZoomY]              Zoom in the x and y vector/n");
	printf("              [-m MoveX,MoveY]              Move in the x and y vector/n");
}
int String2Int(char *Sce,int Len,int *Result)
{
	int j=0;
	int Countflag=0;
	int Countindex=0;
	j=0;
	while(j<Len)
	{
		if(Sce[j]<58&&Sce[j]>46)
		{	
			if(Countflag==0)
			{
				Result[Countindex]=Sce[j]-48;
				Countflag=1;
			}
			else
			{	Result[Countindex]=Result[Countindex]*10+Sce[j]-48;
			}
		}
		else if(Sce[j]==',')
		{
			Countindex++;
			Countflag=0;
		}
		j++;
	}
	return Countindex+1;
}